package com.batsoft.trade.api.web.filter;

import com.gomyb.utils.HttpUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * 解决跨域
 */
@Log4j2
@Component
@Order(100)
public class CorsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse rep, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) rep;
        HttpServletRequest request = (HttpServletRequest) req;
        HttpUtils.setResponse(request, response);
        log.debug("允许跨域");
        chain.doFilter(req, rep);
    }

    @Override
    public void destroy() {
    }
}
